﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FrameworkLib.Configurations.FormsConfiguration.BL;
using FrameworkLib.Windows.Forms;

namespace FrameworkLib.Configurations.FormsConfiguration
{
    public partial class ColumnsConfigurationManager : Form, IForm
    {
        public List<BL.ColumnConfiguration> Columns { get; set; }
        public string FormName { get; set; }
        public string FormTitle { get; set; }
        public byte[] State { get; set; }
        public SavingConfigurationBag Configuration { get; set; }

        Facade.FormsConfigurationFacade fcf = null;

        public ColumnsConfigurationManager()
        {
            InitializeComponent();

            this.btnOk.Click += new EventHandler(btnOk_Click);
            this.btnCancel.Click += new EventHandler(btnCancel_Click);

            fcf = new Facade.FormsConfigurationFacade();
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void btnOk_Click(object sender, EventArgs e)
        {
            SaveConfiguration();
        }

        private void SaveConfiguration()
        {
            List<BL.ColumnConfiguration> columns = (List<BL.ColumnConfiguration>)this.dataGridView1.DataSource;

            fcf.SaveColumns(columns);
        }

        public void InitializeForm()
        {
            this.lblFormName.Text = FormTitle;

            FormConfiguration fc = fcf.GetForm(this.FormName);
            if (fc == null)
            {
                fc = Facade.FormsConfigurationFacade.CreateFormConfiguration(this.FormName);
            }

            foreach (ColumnConfiguration cc in Columns)
            {
                cc.Owner = fc;
            }

            this.dataGridView1.DataSource = Columns;
        }

        public List<BL.ColumnConfiguration> GetColumns(string formName)
        {
            List<BL.ColumnConfiguration> retValue = fcf.GetColumns(formName);

            return retValue;
        }

        //public void SaveState()
        //{
        //    SavingConfigurationBag scb = new SavingConfigurationBag();
        //    scb.FormName = this.FormName;
        //    if (fcf.SaveState(scb, this.State))
        //    {
        //        MessageBox.Show("Salvataggio configurazione completato!", "Gestione Polveri", MessageBoxButtons.OK, MessageBoxIcon.Information);
        //    }
        //    else
        //    {
        //        string msg = string.Format("Salvataggio configurazione non riuscito!\n{0}", fcf.ErrorOccurred);
        //        MessageBox.Show(msg, "Gestione Polveri", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        //    }
        //}

        public void SaveState()
        {
            if (fcf.SaveState(this.Configuration, this.State))
            {
                MessageBox.Show("Salvataggio configurazione completato!", "Gestione Polveri", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                string msg = string.Format("Salvataggio configurazione non riuscito!\n{0}", fcf.ErrorOccurred);
                MessageBox.Show(msg, "Gestione Polveri", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        public byte[] GetState(string formName)
        {
            return fcf.GetState(formName);
        }

        public byte[] GetState(SavingConfigurationBag configuration)
        {
            return fcf.GetState(configuration);
        }
    }
}
